package org.eclipse.moquette.server.netty;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * 处理超时等心态问题
 * Handler should handle the IdleStateEvent triggered by IdleStateHandler.
 * Created by alex on 15-5-30.
 */
public class MoquetteIdleTimoutHandler extends ChannelDuplexHandler {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if (evt instanceof IdleStateEvent) {
            IdleState e = ((IdleStateEvent) evt).state();
            if (e == IdleState.ALL_IDLE) {
                //fire a chanelInactive to trigger publish of will
                ctx.fireChannelInactive();
                ctx.close();
            }
        }
    }
}
